<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编辑者" prop="editor">
        <el-input
          v-model="queryParams.editor"
          placeholder="请输入编辑者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fetv:audit:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fetv:audit:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fetv:audit:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="来源" align="center" prop="comeFrom"/>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fetv_audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="资讯类别" align="center" prop="informationCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fetv_information_category" :value="scope.row.informationCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="资讯栏目" align="center" prop="navigationContextId">
        <template slot-scope="scope">
          <div v-async-context-name="scope.row.navigationContextId"></div>
        </template>
      </el-table-column>
      <el-table-column label="编辑者" align="center" prop="editor"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fetv_yes_no" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['fetv:audit:list']"
          >详情
          </el-button>
          <el-button
            v-if="scope.row.auditStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row,true)"
            v-hasPermi="['fetv:audit:edit']"
          >通过
          </el-button>
          <el-button
            v-if="scope.row.auditStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row,false)"
            v-hasPermi="['fetv:audit:edit']"
          >拒绝
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fetv:audit:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleStatus(scope.row)"
            v-hasPermi="['fetv:school:edit']"
          >
            <div v-if="scope.row.status == 0">禁用</div>
            <div v-else>启用</div>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px" :disabled="title==='详情'">
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="来源" prop="comeFrom">
          <el-input
            v-model="form.comeFrom"
            placeholder="请输入来源"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否主菜" prop="isPrimary">
          <el-radio-group v-model="form.isPrimary">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model="form.sort"
            placeholder="请输入排序"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <image-upload
            v-model="form.icon"
            :limit="1"
          />
        </el-form-item>

        <el-form-item label="资讯类别" prop="informationCategory">
          <el-select v-model="form.informationCategory" placeholder="请选择资讯类别">
            <el-option
              v-for="dict in dict.type.fetv_information_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="略缩图" prop="slightlyThumbnail">
          <image-upload
            v-model="form.slightlyThumbnail"
            :limit="1"
          />
        </el-form-item>
        <el-form-item label="资讯栏目" prop="navigationContextId">
          <el-select
            v-model="form.navigationContextId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择资讯栏目"
            :remote-method="fetchParentDictLabel"
            @focus="fetchParentDictLabel('')"
            :loading="SelectLoading">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否推荐" prop="isRecommend">
          <el-radio-group v-model="form.isRecommend">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否轮播推荐" prop="isCarouselRecommend">
          <el-radio-group v-model="form.isCarouselRecommend">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-radio-group v-model="form.isTop">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否搜索推荐" prop="isSearchRecommend">
          <el-radio-group v-model="form.isSearchRecommend">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="标签" prop="labels">
          <el-input
            v-model="form.labels"
            placeholder="请输入标签"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所属学校ID" prop="schoolId">
          <el-select
            @focus="fetchSchoolDictLabel('')"
            v-model="form.schoolId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入学校"
            :remote-method="fetchSchoolDictLabel"
            :loading="SelectLoading">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">开启</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addAudit, delAudit, listAudit, updateAudit} from "@/api/fetv/audit";
import {getContextNameById} from "@/api/fetv/context";
import {getInformation, updateInformation} from "@/api/fetv/information";

export default {
  name: "Audit",
  dicts: ["fetv_information_category", "fetv_no_yes", "fetv_yes_no", "fetv_audit_status"],
  directives: {
    asyncContextName: {
      async bind(el, binding) {
        const result = await getContextNameById(binding.value);
        el.innerText = result;
      },
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审核表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        comeFrom: null,
        auditStatus: null,
        informationCategory: null,
        navigationContextId: null,
        editor: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        comeFrom: [
          {required: true, message: "navigation_context的id不能为空", trigger: "change"}
        ],
        informationCategory: [
          {required: true, message: "资讯类别(字典）不能为空", trigger: "change"}
        ],
        navigationContextId: [
          {required: true, message: "资讯栏目id不能为空", trigger: "change"}
        ],
        editor: [
          {required: true, message: "编辑者不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**详情*/
    handleDetail(row) {
      /** 发送请求*/
      getInformation(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "详情";
      });
    },
    /** 修改状态操作 */
    handleStatus(row) {
      this.reset();
      const id = row.id || this.ids
      /** 发送请求*/
      this.form.id = id;
      this.form.status = row.status ^ 1;
      updateInformation(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      })
    },
    /** 审核*/
    handleAudit(row, flag) {
      this.reset();
      this.form.id = row.id;
      this.form.auditStatus = flag === true ? 1 : 2
      updateAudit(this.form).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      });
    },
    /** 查询审核列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then(response => {
        this.auditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        comeFrom: null,
        auditStatus: null,
        informationCategory: null,
        navigationContextId: null,
        editor: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审核";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除审核编号为"' + ids + '"的数据项？').then(function () {
        return delAudit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fetv/audit/export', {
        ...this.queryParams
      }, `audit_${new Date().getTime()}.xlsx`)
    },
    //   资讯类别字典翻译
    informationCategoryFormat(row, column) {
      return this.selectDictLabel(this.dict.type.fetv_information_category, row.informationCategory);
    },
  }
}
</script>
